package com.hou.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hou.mapper.GradeMapper;
import com.hou.mapper.UserMapper;
import com.hou.pojo.Grade;
import com.hou.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @author shkstart
 * @create 2021-09-29-9:14
 */
@Service
public class GradeServiceImpl extends ServiceImpl<GradeMapper, Grade> implements GradeService {
    @Autowired
    GradeMapper gradeMapper;

    @Autowired
    UserMapper userMapper;

    // 宇宙成绩
    @Override
    public List getAllGrade() {
        List<Grade> grades = gradeMapper.selectList(null);

        // 遍历获取用户昵称
        for (Grade grade : grades) {
            Integer userId = grade.getUserId();
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", userId);
            User user = userMapper.selectOne(queryWrapper);
            grade.setUserName(user.getUserName());
        }
        return grades;
    }

    // 用户成绩
    @Override
    public List getUserGrade(Integer id) {
        QueryWrapper<Grade> queryWrapperGrade = new QueryWrapper<>();
        queryWrapperGrade.eq("user_id",id);
        List<Grade> grades = gradeMapper.selectList(queryWrapperGrade);

        QueryWrapper<User> queryWrapperUser = new QueryWrapper<>();
        queryWrapperUser.eq("id",id);
        User user = userMapper.selectOne(queryWrapperUser);

        for (Grade grade : grades) {
            grade.setUserName(user.getUserName());
        }
        return grades;
    }

    @Override
    public Boolean addGrade(Grade grade) {
        try {
            gradeMapper.insert(grade);
            return true;
        } catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
